package com.naiterui.ehp.bs.patient.handler.data;

import com.naiterui.ehp.bp.bo.im.PatientMsgSendParamBO;
import com.naiterui.ehp.bp.utils.event.BizEvent;
import com.naiterui.ehp.bs.wechat.constants.WeChatConstant;
import lombok.Getter;
import lombok.ToString;

import java.util.Collections;
import java.util.Map;

/**
 * @author gaoliangliang
 * @date 2019-07-12 13:55
 * @since 1.0.0
 */
@Getter
@ToString(callSuper = true)
public class WxMsgEvent extends BizEvent<WxMsgData> {

    private static final long serialVersionUID = 5454482112601283222L;
    private int msgType;

    public WxMsgEvent(Map<String, String> wxSource) {
        super(wxSource, WxMsgData
                        .builder()
                        .msgType(getMsgType(wxSource.get("MsgType")))
                        .msgId(wxSource.get("MsgId"))
                        .mediaId(wxSource.get("MediaId"))
                        .thumbMediaId(wxSource.get("ThumbMediaId"))
                        .openId(wxSource.get("FromUserName"))
                        .content(wxSource.get("Content"))
                        .picUrl(wxSource.get("PicUrl"))
                        .build(),
                BizEvent.EVENT_TYPE_WXMSG);
        msgType = eventData.getMsgType();
    }

    public WxMsgEvent(String openId, String content, String extId, Integer msgType) {
        super(Collections.emptyMap(), WxMsgData
                        .builder()
                        .msgType(msgType)
                        .extId(extId)
                        .msgId(System.currentTimeMillis() + "")
                        .openId(openId)
                        .content(content)
                        .build(),
                BizEvent.EVENT_TYPE_WXMSG);
        this.msgType = eventData.getMsgType();
    }

    private static int getMsgType(String msgType) {
        int type;
        switch (msgType) {
            case WeChatConstant.XmlMsgType.TEXT:
                type = PatientMsgSendParamBO.MsgType.TEXT;
                break;
            case WeChatConstant.XmlMsgType.IMAGE:
                type = PatientMsgSendParamBO.MsgType.IMAGE;
                break;
            case WeChatConstant.XmlMsgType.VOICE:
                type = PatientMsgSendParamBO.MsgType.VOICE;
                break;
            case WeChatConstant.XmlMsgType.VIDEO:
                type = PatientMsgSendParamBO.MsgType.VIDEO;
                break;
            case WeChatConstant.XmlMsgType.SHORTVIDEO:
                type = PatientMsgSendParamBO.MsgType.SHORTVIDEO;
                break;
            default:
                type = -1;
                break;
        }
        return type;
    }

    @Override
    public Map<String, String> getSource() {
        return (Map<String, String>) super.getSource();
    }
}